package net.oschina.victor.liu.lemon.controller;

import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.codehaus.jackson.map.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import net.oschina.victor.liu.lemon.common.MessageException;
import net.oschina.victor.liu.lemon.entity.Role;
import net.oschina.victor.liu.lemon.entity.User;
import net.oschina.victor.liu.lemon.service.RoleService;
import net.oschina.victor.liu.lemon.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(@RequestParam(value = "pageNo", required = false, defaultValue = "0") String pageNoStr,
			@RequestParam(value = "pageSize", required = false, defaultValue = "10") String pageSizeStr, String keyword,
			Model model) {
		int pageNo = 0;
		try {
			pageNo = Integer.parseInt(pageNoStr);
			if (pageNo < 0) {
				pageNo = 0;
			}
		} catch (NumberFormatException e) {
			pageNo = 0;
			e.printStackTrace();
		}
		int pageSize = 0;
		try {
			pageSize = Integer.parseInt(pageSizeStr);
			if (pageSize < 0) {
				pageSize = 10;
			}
		} catch (NumberFormatException e) {
			pageSize = 10;
			e.printStackTrace();
		}
		if (keyword == null) {
			keyword = "";
		}

		model.addAttribute("keyword", keyword);
		Page<User> page = userService.list(keyword, pageNo, pageSize);
		model.addAttribute("page", page);
		return "user/index";
	}

	/**
	 * 1、接受RESTful中的ID信息，并将其转换为Long类型变量<br/>
	 * 2、查询所有备选角色信息列表<br/>
	 * 3、通过用户ID查询到用户信息及用户角色关联的角色信息列表<br/>
	 * 4、通过用户关联角色信息列表，将备选角色信息列表中角色标记为选中状态<br/>
	 * 
	 * @param idStr
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String input(@PathVariable("id") String idStr, Map<String, Object> map) {
		List<Role> roles = roleService.listAll();
		try {
			Long id = Long.parseLong(idStr);
			if (id != null) {
				User user = userService.getUser(id);
				map.put("user", user);
				for (Role role : roles) {
					if (user.getRoles().contains(role)) {
						role.setIsSelected(true);
					}
				}
				map.put("roles", roles);
			}
		} catch (NumberFormatException e) {
		}
		return "user/edit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String postUser(User user, String[] roleIds) {
		try {
			if (user == null || "".equals(user.getUsername())) {
				return "edit";
			} else {
				user.setCreateDate(new Date(0));
				userService.save(user, roleIds);
			}
		} catch (MessageException e) {
			e.printStackTrace();
		}
		return "redirect:/user/list";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
	public String putUser(User user, String[] roleIds) {
		System.out.println("获取的UserID" + user.getId());
		try {
			if (user == null || "".equals(user.getUsername())) {
				System.out.println("出现错误了");
				return "edit";
			} else {
				User dest = user;
				System.out.println("接收到的" + dest);
				User orig = userService.getUser(user.getId());
				System.out.println("数据库查询的" + orig);
				try {
					BeanUtils.copyProperties(orig, dest);

					System.out.println("dest的" + dest);
					System.out.println("orig的" + orig);

				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				user.setUpdateDate(new Date(0));
				userService.update(user, roleIds);
			}
		} catch (MessageException e) {
			e.printStackTrace();
		}
		return "redirect:/user/list";
	}

}
